package top.system.dept.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.system.dept.mapper.DeptMapper;
import top.system.dept.pojo.Dept;
import top.system.dept.pojo.SysResp;
import top.system.dept.utils.RespUtil;

/**
 * @ClassName: DeptService
 * @Author: Amosen
 * @Since: 2021/5/22
 */

@Service
public class DeptService {

    @Autowired
    DeptMapper deptMapper;

    @Autowired
    UserService userService;

    public SysResp getDept(boolean all, Integer deptId) {
        if (all) {
            return RespUtil.successData("depts", deptMapper.getAllDept());
        } else {
            if (deptId == null || deptId < 0) {
                return RespUtil.illegalParam();
            } else {
                return RespUtil.successData("dept", deptMapper.getDeptById(deptId));
            }
        }
    }

    public SysResp addDept(String userModal, Dept dept) {
        if (!userService.canUseManager(userModal)) {
            return RespUtil.fail();
        }
        deptMapper.addDept(dept);
        return RespUtil.success();
    }

    public SysResp updateDept(String userModal, Dept dept) {
        if (!userService.canUseManager(userModal)) {
            return RespUtil.fail();
        }
        deptMapper.updateDept(dept);
        return RespUtil.success();
    }

    public SysResp deleteDept(String userModal, Dept dept) {
        if (!userService.canUseManager(userModal)) {
            return RespUtil.fail();
        }
        deptMapper.deleteDept(dept);
        return RespUtil.success();
    }
}
